package com.feiyisys.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyisys.entity.Feedback;
import com.feiyisys.mapper.FeedbackMapper;
import com.feiyisys.service.FeedbackService;
import com.feiyisys.dto.FeedbackQueryDTO;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 用户反馈表 服务实现类
 * </p>
 *
 * @author system
 * @since 2025-10-14
 */
@Service
@Slf4j
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Override
    public boolean submitFeedback(Feedback feedback) throws IOException {
        // 验证反馈标题和内容不能为空
        validateFeedback(feedback);

        // 设置默认状态为未处理
        feedback.setStatus((byte) 0);
        feedback.setCreateTime(new Date());

        return save(feedback);
    }

    @Override
    public IPage<Feedback> getUserFeedbacks(FeedbackQueryDTO queryDTO) {
        Page<Feedback> page = new Page<>(queryDTO.getPage(), queryDTO.getSize());
        return getBaseMapper().selectUserFeedbacks(page, queryDTO.getUserId(), queryDTO.getTitle());
    }

    @Override
    public IPage<Feedback> getAllFeedbacks(FeedbackQueryDTO queryDTO) {
        Page<Feedback> page = new Page<>(queryDTO.getPage(), queryDTO.getSize());
        return getBaseMapper().selectAllFeedbacks(page, queryDTO.getTitle(), queryDTO.getStatus());
    }

    @Override
    public boolean replyFeedback(Long feedbackId, String replyContent) {
        Feedback feedback = getById(feedbackId);
        if (feedback == null) {
            return false;
        }

        feedback.setReplyContent(replyContent);
        feedback.setReplyTime(new Date());
        feedback.setStatus((byte) 2); // 已回复状态

        return updateById(feedback);
    }

    @Override
    public boolean updateFeedbackStatus(Long feedbackId, Byte status) {
        Feedback feedback = getById(feedbackId);
        if (feedback == null) {
            return false;
        }

        feedback.setStatus(status);
        // 如果是已处理状态且未回复，更新处理时间
        if (status == 1 && feedback.getReplyTime() == null) {
            feedback.setReplyTime(new Date());
        }

        return updateById(feedback);
    }

    @Override
    public boolean deleteFeedback(Long feedbackId, Long userId) {
        Feedback feedback = getById(feedbackId);
        if (feedback == null) {
            return false;
        }

        // 普通用户只能删除自己的反馈，userId为0表示管理员
        if (userId != 0 && !feedback.getUserId().equals(userId)) {
            return false;
        }

        return removeById(feedbackId);
    }

    @Override
    public boolean updateFeedback(Feedback feedback) {
        // 验证反馈对象的有效性
        if (feedback == null || feedback.getId() == null) {
            log.error("编辑反馈失败：反馈ID不能为空");
            return false;
        }



        // 获取数据库中的现有反馈记录
        Feedback existingFeedback = getById(feedback.getId());
        if (existingFeedback == null) {
            log.error("编辑反馈失败：反馈不存在，ID: {}", feedback.getId());
            return false;
        }

        // 更新允许编辑的字段
        existingFeedback.setTitle(feedback.getTitle());
        existingFeedback.setContent(feedback.getContent());
        existingFeedback.setContactInfo(feedback.getContactInfo());

        boolean success = updateById(existingFeedback);
        log.info("编辑反馈{}", success ? "成功" : "失败" + "，ID: {}", feedback.getId());
        return success;
    }

    /**
     * 验证反馈对象的有效性
     */
    private void validateFeedback(Feedback feedback) throws IOException {
        if (feedback == null) {
            log.error("反馈对象不能为空");
            throw new IOException("反馈对象不能为空");
        }
        if (feedback.getTitle() == null || feedback.getTitle().trim().isEmpty()) {
            log.error("反馈标题不能为空");
            throw new IOException("反馈标题不能为空");
        }
        if (feedback.getContent() == null || feedback.getContent().trim().isEmpty()) {
            log.error("反馈内容不能为空");
            throw new IOException("反馈内容不能为空");
        }
    }
}
